﻿using System;
using System.Diagnostics;
using System.Net;
using System.Text;
using Golos.Helper;

namespace Golos.Http
{
    public abstract class HttpClientBase
    {
        protected HttpClientBase(Uri serverUri)
        {
            this.serverUri = serverUri;
        }

        protected HttpClientBase()
        {
            
        }

        public HttpWebResponse SendRequest(string requestUrl, string method, params byte[] data)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(serverUri+requestUrl);
            LogClass.WriteInfoEntry(string.Format("Send request: {0}\t{1}", method, req.RequestUri));
            req.Method = method;
            if (data != null && data.Length != 0)
            {
                req.ContentLength = data.Length;
                req.GetRequestStream().Write(data, 0, data.Length);
            }
            else
                req.ContentLength = 0;
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            stopwatch.Stop();
            LogClass.WriteInfoEntry(string.Format("Complete request: {0}\t{1}\t{2}\t{3}", method, req.RequestUri, response.StatusCode, stopwatch.Elapsed));
            if (response.StatusCode != HttpStatusCode.OK)
            {
                response.Close();
                throw new Exception(string.Format("Invalid response status code {0}", response.StatusCode));
            }
            return response;
        }

        public HttpResponse TrySendRequest(string requestUrl, string method, params byte[] data)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            HttpResponse result = null;
            HttpWebResponse response = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format("{0}{1}", serverUri, requestUrl));
                LogClass.WriteInfoEntry(string.Format("Send request: {0}\t{1}", method, request.RequestUri));
                request.Method = method;
                if (data != null && data.Length != 0)
                {
                    request.ContentLength = data.Length;
                    request.GetRequestStream().Write(data, 0, data.Length);
                }
                else
                    request.ContentLength = 0;
                response = (HttpWebResponse)request.GetResponse();
                result = new HttpResponse(response.GetResponseStream(), response.ContentLength, response.ContentEncoding, response.StatusCode);
                stopwatch.Stop();
                LogClass.WriteInfoEntry(string.Format("Complete request: {0}\t{1}\t{2}\t{3}", method, request.RequestUri, response.StatusCode, stopwatch.Elapsed));
            }
            catch (Exception exception)
            {
                stopwatch.Stop();
                LogClass.WriteErrorEntry(string.Format("Error in request: {0}\t{1}\t{2}\r\n{3}", method, requestUrl, stopwatch.Elapsed, exception));
            }
            finally
            {
                if (response != null) 
                    response.Close();
            }
            return result;
        }

        protected Uri serverUri;

        
    }
}
